using System;
using System.IO;
using System.Windows.Forms;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;
using UseCaseManager.Logic;
using DialogResult = Habanero.UI.Base.DialogResult;
using MessageBoxButtons = Habanero.UI.Base.MessageBoxButtons;
using MessageBoxIcon = Habanero.UI.Base.MessageBoxIcon;

namespace UseCaseManager.UI
{
    /// <summary>
    /// A read-only grid control used for adding and editing Projects.
    /// </summary>
    public class ProjectsGridControl : UserControlWin, IFormControl
    {
        private readonly IControlFactory _controlFactory;
        public IButton SetBaseline;
        public IButton GenerateProjectDocument;
        public ProjectsGridControl(IControlFactory controlFactory)
        {
            _controlFactory = controlFactory;

            // Create the read-only grid
            projectGridControl = _controlFactory.CreateEditableGridControl();

            // Used to specify a custom UI def
            projectGridControl.Initialise(ClassDef.Get<Project>(), "default");

            // Load the collection of Projects.  You can set the criteria and order by values here.
            BusinessObjectCollection<Project> sampleCollection = Broker.GetBusinessObjectCollection<Project>("", "");
            projectGridControl.BusinessObjectCollection = sampleCollection;

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            projectGridControl.FilterControl.Visible = false;
            //projectGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            // projectGridControl.Buttons.ShowDefaultDeleteButton = true;

            SetBaseline = projectGridControl.Buttons.AddButton("SetBaseline", @"Set &Baseline",
                                                  ClickHandlerSetBaseline);
            GenerateProjectDocument = projectGridControl.Buttons.AddButton("GenerateProjectDocument",
                                        @"&Generate Document", ClickHandlerGenerateProjectDocument);

            // Add the grid to the control so that it fills the control
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(projectGridControl, BorderLayoutManager.Position.Centre);
        }


        public IEditableGridControl projectGridControl { get; private set; }

        private void ClickHandlerSetBaseline(object sender, EventArgs e)
        {
            Project project = (Project)projectGridControl.SelectedBusinessObject;
            if (project.PointsBaseline == 0)
            {
                SetBaselineAndNotify(project);
            }
            else
            {
                if (
                    GlobalUIRegistry.ControlFactory.ShowMessageBox("Overwrite previous baseline?", "Set Baseline",
                                                                   MessageBoxButtons.OKCancel, MessageBoxIcon.None) ==
                    DialogResult.OK)
                {
                    SetBaselineAndNotify(project);
                }
            }
        }

        private static void SetBaselineAndNotify(Project project)
        {
            project.SetBaseline();
            GlobalUIRegistry.ControlFactory.ShowMessageBox("Baseline is now set.", "Set Baseline",
                                                           MessageBoxButtons.OK, MessageBoxIcon.None);
            project.Save();
        }

        private void ClickHandlerGenerateProjectDocument(object sender, EventArgs e)
        {

            Project project = (Project)projectGridControl.SelectedBusinessObject;
            //jippo to account for caching problem in Habanero- related objects not loading
            //these counts force loading
            int count = project.Packages.Count;
            foreach (Package package in project.Packages)
            {
                foreach (UseCase useCase in package.UseCases)
                {
                    foreach (UseCaseStep useCaseStep in useCase.UseCaseSteps)
                    {

                    }
                }
            }
            string fileName = project.ProjectName;
            WriteDocuments.WriteToXmlFile(project, fileName + ".xml");

            string xslFileName = "ProjectToWordTemplate.xsl";
            xslFileName = Path.Combine(Application.StartupPath, Path.Combine("Templates", xslFileName));
            WriteDocuments.WriteProjectToWordDocument(fileName + ".xml", xslFileName, fileName + ".doc");
            GlobalUIRegistry.ControlFactory.ShowMessageBox("Document '" + fileName + ".doc' has been generated.", "Use Case Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form) { }
    }
}